public class ParticleEmitter {
  
  float durationBetweenCycles, deviationBetweenCycles, durationBetweenEachFrame;
  float previousUpdate, randomDuration, lastFrameTime;
  int currentImage = 0;
  PVector deviation, location;
  PImage images[];
  boolean reset = true;
  SoundEffects sound;
  PApplet p;
  int rand;
  String soundFiles[];
  float xOrigin, yOrigin;
 
  public ParticleEmitter(PImage images[], PVector location, PVector deviation, float durationBetweenCycles, float deviationBetweenCycles, float durationBetweenEachFrame, PApplet p, String soundFiles[]){
    previousUpdate = millis();
    this.durationBetweenCycles = durationBetweenCycles;
    this.deviationBetweenCycles = deviationBetweenCycles;
    this.durationBetweenEachFrame = durationBetweenEachFrame;
    this.images = images;
    this.deviation = deviation;
    randomDuration =  0;//random((durationBetweenCycles - deviationBetweenCycles),(durationBetweenCycles + deviationBetweenCycles)); 
    lastFrameTime = millis();
    location = new PVector(0,0);
    this.soundFiles = soundFiles;
    this.p = p;
    if(soundFiles != null) {
      sound = new SoundEffects(p, this.soundFiles[0], 0);
    }
    xOrigin = location.x;
    yOrigin = location.y;
  }
  
  void setScale(float scaleValue){
    
  }
  
  void updateLocation(float x, float y){
    location.x = x;
    location.y = y;
  }
  
  
  void updateParticles(){
    
    int currentTime = millis();
    if(currentTime - previousUpdate > randomDuration){
      if(reset == true){
        rand = (int)(random(0,600) % soundFiles.length);
        sound.updateSoundFile(soundFiles[rand]);
        sound.setGain(3);
        sound.play();
        randomDuration = random((durationBetweenCycles - deviationBetweenCycles),(durationBetweenCycles + deviationBetweenCycles)); 
        lastFrameTime = currentTime;
        reset = false;
        location.x = random((xOrigin - deviation.x), (xOrigin + deviation.x));
        location.y = random((yOrigin - deviation.y), (yOrigin + deviation.y));
      }
      if(currentImage == (images.length)){
        previousUpdate = currentTime;
      }
      if(currentTime - lastFrameTime > durationBetweenEachFrame){
       lastFrameTime = currentTime;
       if(currentImage == images.length){
         currentImage = 0;
         reset = true;
         sound.cleanUp();
       } else {
         image(images[currentImage], location.x, location.y);
         currentImage++; 
       }
      } else if(currentImage < images.length){
        image(images[currentImage], location.x, location.y); 
      }
    }
  }
}
